package com.nix.web.service.user;


import com.nix.web.databases.pojo.user.Role;
import com.nix.web.databases.pojo.user.User;
import com.nix.web.databases.service.iteratory.Iterator;

import com.nix.web.databases.service.user.UserService;
import com.nix.web.service.constant.IsUseResources;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Create by zhangpe0312@qq.com on 2017/12/15.
 */
@Service
public class UserServiceImpl extends UserService {


    @Autowired
    private UserInfoServiceImpl infoService;

    /**
     * 获取冻结用户的信息
     *
     * @param user
     * @return
     * @throws Exception
     */
    public User getFreezeUserInformation(User user) throws Exception {
        return getUserInformation(user.getId(), IsUseResources.Freeze);
    }

    /**
     * 获取正常用户的信息
     *
     * @param user
     * @return
     * @throws Exception
     */
    public User getNormalUserInformation(User user) throws Exception {
        return getUserInformation(user.getId(), IsUseResources.Normal);
    }

    /**
     * 获取用户的角色
     *
     * @param user
     * @return
     * @throws Exception
     */
    public Role getRole(User user) throws Exception {
        return user.getRole();
    }

    /**
     * 用户注册的所有信息
     *
     * @param user user的所有信息
     * @throws Exception 用户注册信息时发生的错误
     */
    public int register(User user) {
        int isSuccess = 0;
        try {
            add(user);
            infoService.add(user.getInfo());
            isSuccess = register(user.getId(),user.getInfo().getId());
        } catch (Exception e) {
            System.out.println("用户注册失败！" +e);
            try {
                delete(user.getId());
            } catch (Exception e1) {
                System.out.println("回滚用户注册失败！" + e);
            }
            try {
                infoService.delete(user.getInfo().getId());
            } catch (Exception e1) {
                System.out.println("回滚用户信息添加失败！" + e);
            }
        }
        return isSuccess;
    }

    /**
     * 更新账户账号信息
     * @param user 更新后的账户信息
     * @return
     * @throws Exception 更新失败
     */
    public boolean upDataPassword(User user) throws Exception {
        return update(user);
    }

}
